mballoc.c 144 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264
  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 short 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 = 1 << inode->i_blkbits;
  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[16];
  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. .owner = THIS_MODULE,
  2086. .open = ext4_mb_seq_groups_open,
  2087. .read = seq_read,
  2088. .llseek = seq_lseek,
  2089. .release = seq_release,
  2090. };
  2091. static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
  2092. {
  2093. int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
  2094. struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
  2095. BUG_ON(!cachep);
  2096. return cachep;
  2097. }
  2098. /*
  2099. * Allocate the top-level s_group_info array for the specified number
  2100. * of groups
  2101. */
  2102. int ext4_mb_alloc_groupinfo(struct super_block *sb, ext4_group_t ngroups)
  2103. {
  2104. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2105. unsigned size;
  2106. struct ext4_group_info ***new_groupinfo;
  2107. size = (ngroups + EXT4_DESC_PER_BLOCK(sb) - 1) >>
  2108. EXT4_DESC_PER_BLOCK_BITS(sb);
  2109. if (size <= sbi->s_group_info_size)
  2110. return 0;
  2111. size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size);
  2112. new_groupinfo = ext4_kvzalloc(size, GFP_KERNEL);
  2113. if (!new_groupinfo) {
  2114. ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group");
  2115. return -ENOMEM;
  2116. }
  2117. if (sbi->s_group_info) {
  2118. memcpy(new_groupinfo, sbi->s_group_info,
  2119. sbi->s_group_info_size * sizeof(*sbi->s_group_info));
  2120. kvfree(sbi->s_group_info);
  2121. }
  2122. sbi->s_group_info = new_groupinfo;
  2123. sbi->s_group_info_size = size / sizeof(*sbi->s_group_info);
  2124. ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
  2125. sbi->s_group_info_size);
  2126. return 0;
  2127. }
  2128. /* Create and initialize ext4_group_info data for the given group. */
  2129. int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
  2130. struct ext4_group_desc *desc)
  2131. {
  2132. int i;
  2133. int metalen = 0;
  2134. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2135. struct ext4_group_info **meta_group_info;
  2136. struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
  2137. /*
  2138. * First check if this group is the first of a reserved block.
  2139. * If it's true, we have to allocate a new table of pointers
  2140. * to ext4_group_info structures
  2141. */
  2142. if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
  2143. metalen = sizeof(*meta_group_info) <<
  2144. EXT4_DESC_PER_BLOCK_BITS(sb);
  2145. meta_group_info = kmalloc(metalen, GFP_NOFS);
  2146. if (meta_group_info == NULL) {
  2147. ext4_msg(sb, KERN_ERR, "can't allocate mem "
  2148. "for a buddy group");
  2149. goto exit_meta_group_info;
  2150. }
  2151. sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
  2152. meta_group_info;
  2153. }
  2154. meta_group_info =
  2155. sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
  2156. i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
  2157. meta_group_info[i] = kmem_cache_zalloc(cachep, GFP_NOFS);
  2158. if (meta_group_info[i] == NULL) {
  2159. ext4_msg(sb, KERN_ERR, "can't allocate buddy mem");
  2160. goto exit_group_info;
  2161. }
  2162. set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
  2163. &(meta_group_info[i]->bb_state));
  2164. /*
  2165. * initialize bb_free to be able to skip
  2166. * empty groups without initialization
  2167. */
  2168. if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  2169. meta_group_info[i]->bb_free =
  2170. ext4_free_clusters_after_init(sb, group, desc);
  2171. } else {
  2172. meta_group_info[i]->bb_free =
  2173. ext4_free_group_clusters(sb, desc);
  2174. }
  2175. INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
  2176. init_rwsem(&meta_group_info[i]->alloc_sem);
  2177. meta_group_info[i]->bb_free_root = RB_ROOT;
  2178. meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
  2179. #ifdef DOUBLE_CHECK
  2180. {
  2181. struct buffer_head *bh;
  2182. meta_group_info[i]->bb_bitmap =
  2183. kmalloc(sb->s_blocksize, GFP_NOFS);
  2184. BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
  2185. bh = ext4_read_block_bitmap(sb, group);
  2186. BUG_ON(IS_ERR_OR_NULL(bh));
  2187. memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
  2188. sb->s_blocksize);
  2189. put_bh(bh);
  2190. }
  2191. #endif
  2192. return 0;
  2193. exit_group_info:
  2194. /* If a meta_group_info table has been allocated, release it now */
  2195. if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
  2196. kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
  2197. sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] = NULL;
  2198. }
  2199. exit_meta_group_info:
  2200. return -ENOMEM;
  2201. } /* ext4_mb_add_groupinfo */
  2202. static int ext4_mb_init_backend(struct super_block *sb)
  2203. {
  2204. ext4_group_t ngroups = ext4_get_groups_count(sb);
  2205. ext4_group_t i;
  2206. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2207. int err;
  2208. struct ext4_group_desc *desc;
  2209. struct kmem_cache *cachep;
  2210. err = ext4_mb_alloc_groupinfo(sb, ngroups);
  2211. if (err)
  2212. return err;
  2213. sbi->s_buddy_cache = new_inode(sb);
  2214. if (sbi->s_buddy_cache == NULL) {
  2215. ext4_msg(sb, KERN_ERR, "can't get new inode");
  2216. goto err_freesgi;
  2217. }
  2218. /* To avoid potentially colliding with an valid on-disk inode number,
  2219. * use EXT4_BAD_INO for the buddy cache inode number. This inode is
  2220. * not in the inode hash, so it should never be found by iget(), but
  2221. * this will avoid confusion if it ever shows up during debugging. */
  2222. sbi->s_buddy_cache->i_ino = EXT4_BAD_INO;
  2223. EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
  2224. for (i = 0; i < ngroups; i++) {
  2225. desc = ext4_get_group_desc(sb, i, NULL);
  2226. if (desc == NULL) {
  2227. ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i);
  2228. goto err_freebuddy;
  2229. }
  2230. if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
  2231. goto err_freebuddy;
  2232. }
  2233. return 0;
  2234. err_freebuddy:
  2235. cachep = get_groupinfo_cache(sb->s_blocksize_bits);
  2236. while (i-- > 0)
  2237. kmem_cache_free(cachep, ext4_get_group_info(sb, i));
  2238. i = sbi->s_group_info_size;
  2239. while (i-- > 0)
  2240. kfree(sbi->s_group_info[i]);
  2241. iput(sbi->s_buddy_cache);
  2242. err_freesgi:
  2243. kvfree(sbi->s_group_info);
  2244. return -ENOMEM;
  2245. }
  2246. static void ext4_groupinfo_destroy_slabs(void)
  2247. {
  2248. int i;
  2249. for (i = 0; i < NR_GRPINFO_CACHES; i++) {
  2250. if (ext4_groupinfo_caches[i])
  2251. kmem_cache_destroy(ext4_groupinfo_caches[i]);
  2252. ext4_groupinfo_caches[i] = NULL;
  2253. }
  2254. }
  2255. static int ext4_groupinfo_create_slab(size_t size)
  2256. {
  2257. static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
  2258. int slab_size;
  2259. int blocksize_bits = order_base_2(size);
  2260. int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
  2261. struct kmem_cache *cachep;
  2262. if (cache_index >= NR_GRPINFO_CACHES)
  2263. return -EINVAL;
  2264. if (unlikely(cache_index < 0))
  2265. cache_index = 0;
  2266. mutex_lock(&ext4_grpinfo_slab_create_mutex);
  2267. if (ext4_groupinfo_caches[cache_index]) {
  2268. mutex_unlock(&ext4_grpinfo_slab_create_mutex);
  2269. return 0; /* Already created */
  2270. }
  2271. slab_size = offsetof(struct ext4_group_info,
  2272. bb_counters[blocksize_bits + 2]);
  2273. cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
  2274. slab_size, 0, SLAB_RECLAIM_ACCOUNT,
  2275. NULL);
  2276. ext4_groupinfo_caches[cache_index] = cachep;
  2277. mutex_unlock(&ext4_grpinfo_slab_create_mutex);
  2278. if (!cachep) {
  2279. printk(KERN_EMERG
  2280. "EXT4-fs: no memory for groupinfo slab cache\n");
  2281. return -ENOMEM;
  2282. }
  2283. return 0;
  2284. }
  2285. int ext4_mb_init(struct super_block *sb)
  2286. {
  2287. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2288. unsigned i, j;
  2289. unsigned offset, offset_incr;
  2290. unsigned max;
  2291. int ret;
  2292. i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
  2293. sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
  2294. if (sbi->s_mb_offsets == NULL) {
  2295. ret = -ENOMEM;
  2296. goto out;
  2297. }
  2298. i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
  2299. sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
  2300. if (sbi->s_mb_maxs == NULL) {
  2301. ret = -ENOMEM;
  2302. goto out;
  2303. }
  2304. ret = ext4_groupinfo_create_slab(sb->s_blocksize);
  2305. if (ret < 0)
  2306. goto out;
  2307. /* order 0 is regular bitmap */
  2308. sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
  2309. sbi->s_mb_offsets[0] = 0;
  2310. i = 1;
  2311. offset = 0;
  2312. offset_incr = 1 << (sb->s_blocksize_bits - 1);
  2313. max = sb->s_blocksize << 2;
  2314. do {
  2315. sbi->s_mb_offsets[i] = offset;
  2316. sbi->s_mb_maxs[i] = max;
  2317. offset += offset_incr;
  2318. offset_incr = offset_incr >> 1;
  2319. max = max >> 1;
  2320. i++;
  2321. } while (i <= sb->s_blocksize_bits + 1);
  2322. spin_lock_init(&sbi->s_md_lock);
  2323. spin_lock_init(&sbi->s_bal_lock);
  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. db = e4b.bd_info;
  2493. /* there are blocks to put in buddy to make them really free */
  2494. count += entry->efd_count;
  2495. count2++;
  2496. ext4_lock_group(sb, entry->efd_group);
  2497. /* Take it out of per group rb tree */
  2498. rb_erase(&entry->efd_node, &(db->bb_free_root));
  2499. mb_free_blocks(NULL, &e4b, entry->efd_start_cluster, entry->efd_count);
  2500. /*
  2501. * Clear the trimmed flag for the group so that the next
  2502. * ext4_trim_fs can trim it.
  2503. * If the volume is mounted with -o discard, online discard
  2504. * is supported and the free blocks will be trimmed online.
  2505. */
  2506. if (!test_opt(sb, DISCARD))
  2507. EXT4_MB_GRP_CLEAR_TRIMMED(db);
  2508. if (!db->bb_free_root.rb_node) {
  2509. /* No more items in the per group rb tree
  2510. * balance refcounts from ext4_mb_free_metadata()
  2511. */
  2512. put_page(e4b.bd_buddy_page);
  2513. put_page(e4b.bd_bitmap_page);
  2514. }
  2515. ext4_unlock_group(sb, entry->efd_group);
  2516. kmem_cache_free(ext4_free_data_cachep, entry);
  2517. ext4_mb_unload_buddy(&e4b);
  2518. mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
  2519. }
  2520. int __init ext4_init_mballoc(void)
  2521. {
  2522. ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
  2523. SLAB_RECLAIM_ACCOUNT);
  2524. if (ext4_pspace_cachep == NULL)
  2525. return -ENOMEM;
  2526. ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
  2527. SLAB_RECLAIM_ACCOUNT);
  2528. if (ext4_ac_cachep == NULL) {
  2529. kmem_cache_destroy(ext4_pspace_cachep);
  2530. return -ENOMEM;
  2531. }
  2532. ext4_free_data_cachep = KMEM_CACHE(ext4_free_data,
  2533. SLAB_RECLAIM_ACCOUNT);
  2534. if (ext4_free_data_cachep == NULL) {
  2535. kmem_cache_destroy(ext4_pspace_cachep);
  2536. kmem_cache_destroy(ext4_ac_cachep);
  2537. return -ENOMEM;
  2538. }
  2539. return 0;
  2540. }
  2541. void ext4_exit_mballoc(void)
  2542. {
  2543. /*
  2544. * Wait for completion of call_rcu()'s on ext4_pspace_cachep
  2545. * before destroying the slab cache.
  2546. */
  2547. rcu_barrier();
  2548. kmem_cache_destroy(ext4_pspace_cachep);
  2549. kmem_cache_destroy(ext4_ac_cachep);
  2550. kmem_cache_destroy(ext4_free_data_cachep);
  2551. ext4_groupinfo_destroy_slabs();
  2552. }
  2553. /*
  2554. * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
  2555. * Returns 0 if success or error code
  2556. */
  2557. static noinline_for_stack int
  2558. ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
  2559. handle_t *handle, unsigned int reserv_clstrs)
  2560. {
  2561. struct buffer_head *bitmap_bh = NULL;
  2562. struct ext4_group_desc *gdp;
  2563. struct buffer_head *gdp_bh;
  2564. struct ext4_sb_info *sbi;
  2565. struct super_block *sb;
  2566. ext4_fsblk_t block;
  2567. int err, len;
  2568. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  2569. BUG_ON(ac->ac_b_ex.fe_len <= 0);
  2570. sb = ac->ac_sb;
  2571. sbi = EXT4_SB(sb);
  2572. bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
  2573. if (IS_ERR(bitmap_bh)) {
  2574. err = PTR_ERR(bitmap_bh);
  2575. bitmap_bh = NULL;
  2576. goto out_err;
  2577. }
  2578. BUFFER_TRACE(bitmap_bh, "getting write access");
  2579. err = ext4_journal_get_write_access(handle, bitmap_bh);
  2580. if (err)
  2581. goto out_err;
  2582. err = -EIO;
  2583. gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
  2584. if (!gdp)
  2585. goto out_err;
  2586. ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
  2587. ext4_free_group_clusters(sb, gdp));
  2588. BUFFER_TRACE(gdp_bh, "get_write_access");
  2589. err = ext4_journal_get_write_access(handle, gdp_bh);
  2590. if (err)
  2591. goto out_err;
  2592. block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  2593. len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
  2594. if (!ext4_data_block_valid(sbi, block, len)) {
  2595. ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
  2596. "fs metadata", block, block+len);
  2597. /* File system mounted not to panic on error
  2598. * Fix the bitmap and repeat the block allocation
  2599. * We leak some of the blocks here.
  2600. */
  2601. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  2602. ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
  2603. ac->ac_b_ex.fe_len);
  2604. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  2605. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  2606. if (!err)
  2607. err = -EAGAIN;
  2608. goto out_err;
  2609. }
  2610. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  2611. #ifdef AGGRESSIVE_CHECK
  2612. {
  2613. int i;
  2614. for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
  2615. BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
  2616. bitmap_bh->b_data));
  2617. }
  2618. }
  2619. #endif
  2620. ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
  2621. ac->ac_b_ex.fe_len);
  2622. if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
  2623. gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
  2624. ext4_free_group_clusters_set(sb, gdp,
  2625. ext4_free_clusters_after_init(sb,
  2626. ac->ac_b_ex.fe_group, gdp));
  2627. }
  2628. len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len;
  2629. ext4_free_group_clusters_set(sb, gdp, len);
  2630. ext4_block_bitmap_csum_set(sb, ac->ac_b_ex.fe_group, gdp, bitmap_bh);
  2631. ext4_group_desc_csum_set(sb, ac->ac_b_ex.fe_group, gdp);
  2632. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  2633. percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len);
  2634. /*
  2635. * Now reduce the dirty block count also. Should not go negative
  2636. */
  2637. if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
  2638. /* release all the reserved blocks if non delalloc */
  2639. percpu_counter_sub(&sbi->s_dirtyclusters_counter,
  2640. reserv_clstrs);
  2641. if (sbi->s_log_groups_per_flex) {
  2642. ext4_group_t flex_group = ext4_flex_group(sbi,
  2643. ac->ac_b_ex.fe_group);
  2644. atomic64_sub(ac->ac_b_ex.fe_len,
  2645. &sbi->s_flex_groups[flex_group].free_clusters);
  2646. }
  2647. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  2648. if (err)
  2649. goto out_err;
  2650. err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
  2651. out_err:
  2652. brelse(bitmap_bh);
  2653. return err;
  2654. }
  2655. /*
  2656. * here we normalize request for locality group
  2657. * Group request are normalized to s_mb_group_prealloc, which goes to
  2658. * s_strip if we set the same via mount option.
  2659. * s_mb_group_prealloc can be configured via
  2660. * /sys/fs/ext4/<partition>/mb_group_prealloc
  2661. *
  2662. * XXX: should we try to preallocate more than the group has now?
  2663. */
  2664. static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
  2665. {
  2666. struct super_block *sb = ac->ac_sb;
  2667. struct ext4_locality_group *lg = ac->ac_lg;
  2668. BUG_ON(lg == NULL);
  2669. ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
  2670. mb_debug(1, "#%u: goal %u blocks for locality group\n",
  2671. current->pid, ac->ac_g_ex.fe_len);
  2672. }
  2673. /*
  2674. * Normalization means making request better in terms of
  2675. * size and alignment
  2676. */
  2677. static noinline_for_stack void
  2678. ext4_mb_normalize_request(struct ext4_allocation_context *ac,
  2679. struct ext4_allocation_request *ar)
  2680. {
  2681. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  2682. int bsbits, max;
  2683. ext4_lblk_t end;
  2684. loff_t size, start_off;
  2685. loff_t orig_size __maybe_unused;
  2686. ext4_lblk_t start;
  2687. struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
  2688. struct ext4_prealloc_space *pa;
  2689. /* do normalize only data requests, metadata requests
  2690. do not need preallocation */
  2691. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  2692. return;
  2693. /* sometime caller may want exact blocks */
  2694. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  2695. return;
  2696. /* caller may indicate that preallocation isn't
  2697. * required (it's a tail, for example) */
  2698. if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
  2699. return;
  2700. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
  2701. ext4_mb_normalize_group_request(ac);
  2702. return ;
  2703. }
  2704. bsbits = ac->ac_sb->s_blocksize_bits;
  2705. /* first, let's learn actual file size
  2706. * given current request is allocated */
  2707. size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
  2708. size = size << bsbits;
  2709. if (size < i_size_read(ac->ac_inode))
  2710. size = i_size_read(ac->ac_inode);
  2711. orig_size = size;
  2712. /* max size of free chunks */
  2713. max = 2 << bsbits;
  2714. #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
  2715. (req <= (size) || max <= (chunk_size))
  2716. /* first, try to predict filesize */
  2717. /* XXX: should this table be tunable? */
  2718. start_off = 0;
  2719. if (size <= 16 * 1024) {
  2720. size = 16 * 1024;
  2721. } else if (size <= 32 * 1024) {
  2722. size = 32 * 1024;
  2723. } else if (size <= 64 * 1024) {
  2724. size = 64 * 1024;
  2725. } else if (size <= 128 * 1024) {
  2726. size = 128 * 1024;
  2727. } else if (size <= 256 * 1024) {
  2728. size = 256 * 1024;
  2729. } else if (size <= 512 * 1024) {
  2730. size = 512 * 1024;
  2731. } else if (size <= 1024 * 1024) {
  2732. size = 1024 * 1024;
  2733. } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
  2734. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2735. (21 - bsbits)) << 21;
  2736. size = 2 * 1024 * 1024;
  2737. } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
  2738. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2739. (22 - bsbits)) << 22;
  2740. size = 4 * 1024 * 1024;
  2741. } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
  2742. (8<<20)>>bsbits, max, 8 * 1024)) {
  2743. start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
  2744. (23 - bsbits)) << 23;
  2745. size = 8 * 1024 * 1024;
  2746. } else {
  2747. start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
  2748. size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb),
  2749. ac->ac_o_ex.fe_len) << bsbits;
  2750. }
  2751. size = size >> bsbits;
  2752. start = start_off >> bsbits;
  2753. /* don't cover already allocated blocks in selected range */
  2754. if (ar->pleft && start <= ar->lleft) {
  2755. size -= ar->lleft + 1 - start;
  2756. start = ar->lleft + 1;
  2757. }
  2758. if (ar->pright && start + size - 1 >= ar->lright)
  2759. size -= start + size - ar->lright;
  2760. end = start + size;
  2761. /* check we don't cross already preallocated blocks */
  2762. rcu_read_lock();
  2763. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2764. ext4_lblk_t pa_end;
  2765. if (pa->pa_deleted)
  2766. continue;
  2767. spin_lock(&pa->pa_lock);
  2768. if (pa->pa_deleted) {
  2769. spin_unlock(&pa->pa_lock);
  2770. continue;
  2771. }
  2772. pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
  2773. pa->pa_len);
  2774. /* PA must not overlap original request */
  2775. BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
  2776. ac->ac_o_ex.fe_logical < pa->pa_lstart));
  2777. /* skip PAs this normalized request doesn't overlap with */
  2778. if (pa->pa_lstart >= end || pa_end <= start) {
  2779. spin_unlock(&pa->pa_lock);
  2780. continue;
  2781. }
  2782. BUG_ON(pa->pa_lstart <= start && pa_end >= end);
  2783. /* adjust start or end to be adjacent to this pa */
  2784. if (pa_end <= ac->ac_o_ex.fe_logical) {
  2785. BUG_ON(pa_end < start);
  2786. start = pa_end;
  2787. } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
  2788. BUG_ON(pa->pa_lstart > end);
  2789. end = pa->pa_lstart;
  2790. }
  2791. spin_unlock(&pa->pa_lock);
  2792. }
  2793. rcu_read_unlock();
  2794. size = end - start;
  2795. /* XXX: extra loop to check we really don't overlap preallocations */
  2796. rcu_read_lock();
  2797. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2798. ext4_lblk_t pa_end;
  2799. spin_lock(&pa->pa_lock);
  2800. if (pa->pa_deleted == 0) {
  2801. pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
  2802. pa->pa_len);
  2803. BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
  2804. }
  2805. spin_unlock(&pa->pa_lock);
  2806. }
  2807. rcu_read_unlock();
  2808. if (start + size <= ac->ac_o_ex.fe_logical &&
  2809. start > ac->ac_o_ex.fe_logical) {
  2810. ext4_msg(ac->ac_sb, KERN_ERR,
  2811. "start %lu, size %lu, fe_logical %lu",
  2812. (unsigned long) start, (unsigned long) size,
  2813. (unsigned long) ac->ac_o_ex.fe_logical);
  2814. BUG();
  2815. }
  2816. BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
  2817. /* now prepare goal request */
  2818. /* XXX: is it better to align blocks WRT to logical
  2819. * placement or satisfy big request as is */
  2820. ac->ac_g_ex.fe_logical = start;
  2821. ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size);
  2822. /* define goal start in order to merge */
  2823. if (ar->pright && (ar->lright == (start + size))) {
  2824. /* merge to the right */
  2825. ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
  2826. &ac->ac_f_ex.fe_group,
  2827. &ac->ac_f_ex.fe_start);
  2828. ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
  2829. }
  2830. if (ar->pleft && (ar->lleft + 1 == start)) {
  2831. /* merge to the left */
  2832. ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
  2833. &ac->ac_f_ex.fe_group,
  2834. &ac->ac_f_ex.fe_start);
  2835. ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
  2836. }
  2837. mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
  2838. (unsigned) orig_size, (unsigned) start);
  2839. }
  2840. static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
  2841. {
  2842. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  2843. if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
  2844. atomic_inc(&sbi->s_bal_reqs);
  2845. atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
  2846. if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
  2847. atomic_inc(&sbi->s_bal_success);
  2848. atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
  2849. if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
  2850. ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
  2851. atomic_inc(&sbi->s_bal_goals);
  2852. if (ac->ac_found > sbi->s_mb_max_to_scan)
  2853. atomic_inc(&sbi->s_bal_breaks);
  2854. }
  2855. if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
  2856. trace_ext4_mballoc_alloc(ac);
  2857. else
  2858. trace_ext4_mballoc_prealloc(ac);
  2859. }
  2860. /*
  2861. * Called on failure; free up any blocks from the inode PA for this
  2862. * context. We don't need this for MB_GROUP_PA because we only change
  2863. * pa_free in ext4_mb_release_context(), but on failure, we've already
  2864. * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
  2865. */
  2866. static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
  2867. {
  2868. struct ext4_prealloc_space *pa = ac->ac_pa;
  2869. struct ext4_buddy e4b;
  2870. int err;
  2871. if (pa == NULL) {
  2872. if (ac->ac_f_ex.fe_len == 0)
  2873. return;
  2874. err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b);
  2875. if (err) {
  2876. /*
  2877. * This should never happen since we pin the
  2878. * pages in the ext4_allocation_context so
  2879. * ext4_mb_load_buddy() should never fail.
  2880. */
  2881. WARN(1, "mb_load_buddy failed (%d)", err);
  2882. return;
  2883. }
  2884. ext4_lock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
  2885. mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start,
  2886. ac->ac_f_ex.fe_len);
  2887. ext4_unlock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
  2888. ext4_mb_unload_buddy(&e4b);
  2889. return;
  2890. }
  2891. if (pa->pa_type == MB_INODE_PA)
  2892. pa->pa_free += ac->ac_b_ex.fe_len;
  2893. }
  2894. /*
  2895. * use blocks preallocated to inode
  2896. */
  2897. static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
  2898. struct ext4_prealloc_space *pa)
  2899. {
  2900. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  2901. ext4_fsblk_t start;
  2902. ext4_fsblk_t end;
  2903. int len;
  2904. /* found preallocated blocks, use them */
  2905. start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
  2906. end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len),
  2907. start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len));
  2908. len = EXT4_NUM_B2C(sbi, end - start);
  2909. ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
  2910. &ac->ac_b_ex.fe_start);
  2911. ac->ac_b_ex.fe_len = len;
  2912. ac->ac_status = AC_STATUS_FOUND;
  2913. ac->ac_pa = pa;
  2914. BUG_ON(start < pa->pa_pstart);
  2915. BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len));
  2916. BUG_ON(pa->pa_free < len);
  2917. pa->pa_free -= len;
  2918. mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
  2919. }
  2920. /*
  2921. * use blocks preallocated to locality group
  2922. */
  2923. static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
  2924. struct ext4_prealloc_space *pa)
  2925. {
  2926. unsigned int len = ac->ac_o_ex.fe_len;
  2927. ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
  2928. &ac->ac_b_ex.fe_group,
  2929. &ac->ac_b_ex.fe_start);
  2930. ac->ac_b_ex.fe_len = len;
  2931. ac->ac_status = AC_STATUS_FOUND;
  2932. ac->ac_pa = pa;
  2933. /* we don't correct pa_pstart or pa_plen here to avoid
  2934. * possible race when the group is being loaded concurrently
  2935. * instead we correct pa later, after blocks are marked
  2936. * in on-disk bitmap -- see ext4_mb_release_context()
  2937. * Other CPUs are prevented from allocating from this pa by lg_mutex
  2938. */
  2939. mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
  2940. }
  2941. /*
  2942. * Return the prealloc space that have minimal distance
  2943. * from the goal block. @cpa is the prealloc
  2944. * space that is having currently known minimal distance
  2945. * from the goal block.
  2946. */
  2947. static struct ext4_prealloc_space *
  2948. ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
  2949. struct ext4_prealloc_space *pa,
  2950. struct ext4_prealloc_space *cpa)
  2951. {
  2952. ext4_fsblk_t cur_distance, new_distance;
  2953. if (cpa == NULL) {
  2954. atomic_inc(&pa->pa_count);
  2955. return pa;
  2956. }
  2957. cur_distance = abs(goal_block - cpa->pa_pstart);
  2958. new_distance = abs(goal_block - pa->pa_pstart);
  2959. if (cur_distance <= new_distance)
  2960. return cpa;
  2961. /* drop the previous reference */
  2962. atomic_dec(&cpa->pa_count);
  2963. atomic_inc(&pa->pa_count);
  2964. return pa;
  2965. }
  2966. /*
  2967. * search goal blocks in preallocated space
  2968. */
  2969. static noinline_for_stack int
  2970. ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
  2971. {
  2972. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  2973. int order, i;
  2974. struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
  2975. struct ext4_locality_group *lg;
  2976. struct ext4_prealloc_space *pa, *cpa = NULL;
  2977. ext4_fsblk_t goal_block;
  2978. /* only data can be preallocated */
  2979. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  2980. return 0;
  2981. /* first, try per-file preallocation */
  2982. rcu_read_lock();
  2983. list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
  2984. /* all fields in this condition don't change,
  2985. * so we can skip locking for them */
  2986. if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
  2987. ac->ac_o_ex.fe_logical >= (pa->pa_lstart +
  2988. EXT4_C2B(sbi, pa->pa_len)))
  2989. continue;
  2990. /* non-extent files can't have physical blocks past 2^32 */
  2991. if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
  2992. (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) >
  2993. EXT4_MAX_BLOCK_FILE_PHYS))
  2994. continue;
  2995. /* found preallocated blocks, use them */
  2996. spin_lock(&pa->pa_lock);
  2997. if (pa->pa_deleted == 0 && pa->pa_free) {
  2998. atomic_inc(&pa->pa_count);
  2999. ext4_mb_use_inode_pa(ac, pa);
  3000. spin_unlock(&pa->pa_lock);
  3001. ac->ac_criteria = 10;
  3002. rcu_read_unlock();
  3003. return 1;
  3004. }
  3005. spin_unlock(&pa->pa_lock);
  3006. }
  3007. rcu_read_unlock();
  3008. /* can we use group allocation? */
  3009. if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
  3010. return 0;
  3011. /* inode may have no locality group for some reason */
  3012. lg = ac->ac_lg;
  3013. if (lg == NULL)
  3014. return 0;
  3015. order = fls(ac->ac_o_ex.fe_len) - 1;
  3016. if (order > PREALLOC_TB_SIZE - 1)
  3017. /* The max size of hash table is PREALLOC_TB_SIZE */
  3018. order = PREALLOC_TB_SIZE - 1;
  3019. goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
  3020. /*
  3021. * search for the prealloc space that is having
  3022. * minimal distance from the goal block.
  3023. */
  3024. for (i = order; i < PREALLOC_TB_SIZE; i++) {
  3025. rcu_read_lock();
  3026. list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
  3027. pa_inode_list) {
  3028. spin_lock(&pa->pa_lock);
  3029. if (pa->pa_deleted == 0 &&
  3030. pa->pa_free >= ac->ac_o_ex.fe_len) {
  3031. cpa = ext4_mb_check_group_pa(goal_block,
  3032. pa, cpa);
  3033. }
  3034. spin_unlock(&pa->pa_lock);
  3035. }
  3036. rcu_read_unlock();
  3037. }
  3038. if (cpa) {
  3039. ext4_mb_use_group_pa(ac, cpa);
  3040. ac->ac_criteria = 20;
  3041. return 1;
  3042. }
  3043. return 0;
  3044. }
  3045. /*
  3046. * the function goes through all block freed in the group
  3047. * but not yet committed and marks them used in in-core bitmap.
  3048. * buddy must be generated from this bitmap
  3049. * Need to be called with the ext4 group lock held
  3050. */
  3051. static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
  3052. ext4_group_t group)
  3053. {
  3054. struct rb_node *n;
  3055. struct ext4_group_info *grp;
  3056. struct ext4_free_data *entry;
  3057. grp = ext4_get_group_info(sb, group);
  3058. n = rb_first(&(grp->bb_free_root));
  3059. while (n) {
  3060. entry = rb_entry(n, struct ext4_free_data, efd_node);
  3061. ext4_set_bits(bitmap, entry->efd_start_cluster, entry->efd_count);
  3062. n = rb_next(n);
  3063. }
  3064. return;
  3065. }
  3066. /*
  3067. * the function goes through all preallocation in this group and marks them
  3068. * used in in-core bitmap. buddy must be generated from this bitmap
  3069. * Need to be called with ext4 group lock held
  3070. */
  3071. static noinline_for_stack
  3072. void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
  3073. ext4_group_t group)
  3074. {
  3075. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  3076. struct ext4_prealloc_space *pa;
  3077. struct list_head *cur;
  3078. ext4_group_t groupnr;
  3079. ext4_grpblk_t start;
  3080. int preallocated = 0;
  3081. int len;
  3082. /* all form of preallocation discards first load group,
  3083. * so the only competing code is preallocation use.
  3084. * we don't need any locking here
  3085. * notice we do NOT ignore preallocations with pa_deleted
  3086. * otherwise we could leave used blocks available for
  3087. * allocation in buddy when concurrent ext4_mb_put_pa()
  3088. * is dropping preallocation
  3089. */
  3090. list_for_each(cur, &grp->bb_prealloc_list) {
  3091. pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
  3092. spin_lock(&pa->pa_lock);
  3093. ext4_get_group_no_and_offset(sb, pa->pa_pstart,
  3094. &groupnr, &start);
  3095. len = pa->pa_len;
  3096. spin_unlock(&pa->pa_lock);
  3097. if (unlikely(len == 0))
  3098. continue;
  3099. BUG_ON(groupnr != group);
  3100. ext4_set_bits(bitmap, start, len);
  3101. preallocated += len;
  3102. }
  3103. mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
  3104. }
  3105. static void ext4_mb_pa_callback(struct rcu_head *head)
  3106. {
  3107. struct ext4_prealloc_space *pa;
  3108. pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
  3109. BUG_ON(atomic_read(&pa->pa_count));
  3110. BUG_ON(pa->pa_deleted == 0);
  3111. kmem_cache_free(ext4_pspace_cachep, pa);
  3112. }
  3113. /*
  3114. * drops a reference to preallocated space descriptor
  3115. * if this was the last reference and the space is consumed
  3116. */
  3117. static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
  3118. struct super_block *sb, struct ext4_prealloc_space *pa)
  3119. {
  3120. ext4_group_t grp;
  3121. ext4_fsblk_t grp_blk;
  3122. /* in this short window concurrent discard can set pa_deleted */
  3123. spin_lock(&pa->pa_lock);
  3124. if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) {
  3125. spin_unlock(&pa->pa_lock);
  3126. return;
  3127. }
  3128. if (pa->pa_deleted == 1) {
  3129. spin_unlock(&pa->pa_lock);
  3130. return;
  3131. }
  3132. pa->pa_deleted = 1;
  3133. spin_unlock(&pa->pa_lock);
  3134. grp_blk = pa->pa_pstart;
  3135. /*
  3136. * If doing group-based preallocation, pa_pstart may be in the
  3137. * next group when pa is used up
  3138. */
  3139. if (pa->pa_type == MB_GROUP_PA)
  3140. grp_blk--;
  3141. grp = ext4_get_group_number(sb, grp_blk);
  3142. /*
  3143. * possible race:
  3144. *
  3145. * P1 (buddy init) P2 (regular allocation)
  3146. * find block B in PA
  3147. * copy on-disk bitmap to buddy
  3148. * mark B in on-disk bitmap
  3149. * drop PA from group
  3150. * mark all PAs in buddy
  3151. *
  3152. * thus, P1 initializes buddy with B available. to prevent this
  3153. * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
  3154. * against that pair
  3155. */
  3156. ext4_lock_group(sb, grp);
  3157. list_del(&pa->pa_group_list);
  3158. ext4_unlock_group(sb, grp);
  3159. spin_lock(pa->pa_obj_lock);
  3160. list_del_rcu(&pa->pa_inode_list);
  3161. spin_unlock(pa->pa_obj_lock);
  3162. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3163. }
  3164. /*
  3165. * creates new preallocated space for given inode
  3166. */
  3167. static noinline_for_stack int
  3168. ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
  3169. {
  3170. struct super_block *sb = ac->ac_sb;
  3171. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3172. struct ext4_prealloc_space *pa;
  3173. struct ext4_group_info *grp;
  3174. struct ext4_inode_info *ei;
  3175. /* preallocate only when found space is larger then requested */
  3176. BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
  3177. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  3178. BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
  3179. pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
  3180. if (pa == NULL)
  3181. return -ENOMEM;
  3182. if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
  3183. int winl;
  3184. int wins;
  3185. int win;
  3186. int offs;
  3187. /* we can't allocate as much as normalizer wants.
  3188. * so, found space must get proper lstart
  3189. * to cover original request */
  3190. BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
  3191. BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
  3192. /* we're limited by original request in that
  3193. * logical block must be covered any way
  3194. * winl is window we can move our chunk within */
  3195. winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
  3196. /* also, we should cover whole original request */
  3197. wins = EXT4_C2B(sbi, ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len);
  3198. /* the smallest one defines real window */
  3199. win = min(winl, wins);
  3200. offs = ac->ac_o_ex.fe_logical %
  3201. EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
  3202. if (offs && offs < win)
  3203. win = offs;
  3204. ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical -
  3205. EXT4_NUM_B2C(sbi, win);
  3206. BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
  3207. BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
  3208. }
  3209. /* preallocation can change ac_b_ex, thus we store actually
  3210. * allocated blocks for history */
  3211. ac->ac_f_ex = ac->ac_b_ex;
  3212. pa->pa_lstart = ac->ac_b_ex.fe_logical;
  3213. pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3214. pa->pa_len = ac->ac_b_ex.fe_len;
  3215. pa->pa_free = pa->pa_len;
  3216. atomic_set(&pa->pa_count, 1);
  3217. spin_lock_init(&pa->pa_lock);
  3218. INIT_LIST_HEAD(&pa->pa_inode_list);
  3219. INIT_LIST_HEAD(&pa->pa_group_list);
  3220. pa->pa_deleted = 0;
  3221. pa->pa_type = MB_INODE_PA;
  3222. mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
  3223. pa->pa_pstart, pa->pa_len, pa->pa_lstart);
  3224. trace_ext4_mb_new_inode_pa(ac, pa);
  3225. ext4_mb_use_inode_pa(ac, pa);
  3226. atomic_add(pa->pa_free, &sbi->s_mb_preallocated);
  3227. ei = EXT4_I(ac->ac_inode);
  3228. grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
  3229. pa->pa_obj_lock = &ei->i_prealloc_lock;
  3230. pa->pa_inode = ac->ac_inode;
  3231. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  3232. list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
  3233. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  3234. spin_lock(pa->pa_obj_lock);
  3235. list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
  3236. spin_unlock(pa->pa_obj_lock);
  3237. return 0;
  3238. }
  3239. /*
  3240. * creates new preallocated space for locality group inodes belongs to
  3241. */
  3242. static noinline_for_stack int
  3243. ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
  3244. {
  3245. struct super_block *sb = ac->ac_sb;
  3246. struct ext4_locality_group *lg;
  3247. struct ext4_prealloc_space *pa;
  3248. struct ext4_group_info *grp;
  3249. /* preallocate only when found space is larger then requested */
  3250. BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
  3251. BUG_ON(ac->ac_status != AC_STATUS_FOUND);
  3252. BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
  3253. BUG_ON(ext4_pspace_cachep == NULL);
  3254. pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
  3255. if (pa == NULL)
  3256. return -ENOMEM;
  3257. /* preallocation can change ac_b_ex, thus we store actually
  3258. * allocated blocks for history */
  3259. ac->ac_f_ex = ac->ac_b_ex;
  3260. pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3261. pa->pa_lstart = pa->pa_pstart;
  3262. pa->pa_len = ac->ac_b_ex.fe_len;
  3263. pa->pa_free = pa->pa_len;
  3264. atomic_set(&pa->pa_count, 1);
  3265. spin_lock_init(&pa->pa_lock);
  3266. INIT_LIST_HEAD(&pa->pa_inode_list);
  3267. INIT_LIST_HEAD(&pa->pa_group_list);
  3268. pa->pa_deleted = 0;
  3269. pa->pa_type = MB_GROUP_PA;
  3270. mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
  3271. pa->pa_pstart, pa->pa_len, pa->pa_lstart);
  3272. trace_ext4_mb_new_group_pa(ac, pa);
  3273. ext4_mb_use_group_pa(ac, pa);
  3274. atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
  3275. grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
  3276. lg = ac->ac_lg;
  3277. BUG_ON(lg == NULL);
  3278. pa->pa_obj_lock = &lg->lg_prealloc_lock;
  3279. pa->pa_inode = NULL;
  3280. ext4_lock_group(sb, ac->ac_b_ex.fe_group);
  3281. list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
  3282. ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
  3283. /*
  3284. * We will later add the new pa to the right bucket
  3285. * after updating the pa_free in ext4_mb_release_context
  3286. */
  3287. return 0;
  3288. }
  3289. static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
  3290. {
  3291. int err;
  3292. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
  3293. err = ext4_mb_new_group_pa(ac);
  3294. else
  3295. err = ext4_mb_new_inode_pa(ac);
  3296. return err;
  3297. }
  3298. /*
  3299. * finds all unused blocks in on-disk bitmap, frees them in
  3300. * in-core bitmap and buddy.
  3301. * @pa must be unlinked from inode and group lists, so that
  3302. * nobody else can find/use it.
  3303. * the caller MUST hold group/inode locks.
  3304. * TODO: optimize the case when there are no in-core structures yet
  3305. */
  3306. static noinline_for_stack int
  3307. ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
  3308. struct ext4_prealloc_space *pa)
  3309. {
  3310. struct super_block *sb = e4b->bd_sb;
  3311. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3312. unsigned int end;
  3313. unsigned int next;
  3314. ext4_group_t group;
  3315. ext4_grpblk_t bit;
  3316. unsigned long long grp_blk_start;
  3317. int err = 0;
  3318. int free = 0;
  3319. BUG_ON(pa->pa_deleted == 0);
  3320. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
  3321. grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit);
  3322. BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
  3323. end = bit + pa->pa_len;
  3324. while (bit < end) {
  3325. bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
  3326. if (bit >= end)
  3327. break;
  3328. next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
  3329. mb_debug(1, " free preallocated %u/%u in group %u\n",
  3330. (unsigned) ext4_group_first_block_no(sb, group) + bit,
  3331. (unsigned) next - bit, (unsigned) group);
  3332. free += next - bit;
  3333. trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
  3334. trace_ext4_mb_release_inode_pa(pa, (grp_blk_start +
  3335. EXT4_C2B(sbi, bit)),
  3336. next - bit);
  3337. mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
  3338. bit = next + 1;
  3339. }
  3340. if (free != pa->pa_free) {
  3341. ext4_msg(e4b->bd_sb, KERN_CRIT,
  3342. "pa %p: logic %lu, phys. %lu, len %lu",
  3343. pa, (unsigned long) pa->pa_lstart,
  3344. (unsigned long) pa->pa_pstart,
  3345. (unsigned long) pa->pa_len);
  3346. ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
  3347. free, pa->pa_free);
  3348. /*
  3349. * pa is already deleted so we use the value obtained
  3350. * from the bitmap and continue.
  3351. */
  3352. }
  3353. atomic_add(free, &sbi->s_mb_discarded);
  3354. return err;
  3355. }
  3356. static noinline_for_stack int
  3357. ext4_mb_release_group_pa(struct ext4_buddy *e4b,
  3358. struct ext4_prealloc_space *pa)
  3359. {
  3360. struct super_block *sb = e4b->bd_sb;
  3361. ext4_group_t group;
  3362. ext4_grpblk_t bit;
  3363. trace_ext4_mb_release_group_pa(sb, pa);
  3364. BUG_ON(pa->pa_deleted == 0);
  3365. ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
  3366. BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
  3367. mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
  3368. atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
  3369. trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
  3370. return 0;
  3371. }
  3372. /*
  3373. * releases all preallocations in given group
  3374. *
  3375. * first, we need to decide discard policy:
  3376. * - when do we discard
  3377. * 1) ENOSPC
  3378. * - how many do we discard
  3379. * 1) how many requested
  3380. */
  3381. static noinline_for_stack int
  3382. ext4_mb_discard_group_preallocations(struct super_block *sb,
  3383. ext4_group_t group, int needed)
  3384. {
  3385. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  3386. struct buffer_head *bitmap_bh = NULL;
  3387. struct ext4_prealloc_space *pa, *tmp;
  3388. struct list_head list;
  3389. struct ext4_buddy e4b;
  3390. int err;
  3391. int busy = 0;
  3392. int free = 0;
  3393. mb_debug(1, "discard preallocation for group %u\n", group);
  3394. if (list_empty(&grp->bb_prealloc_list))
  3395. return 0;
  3396. bitmap_bh = ext4_read_block_bitmap(sb, group);
  3397. if (IS_ERR(bitmap_bh)) {
  3398. err = PTR_ERR(bitmap_bh);
  3399. ext4_error(sb, "Error %d reading block bitmap for %u",
  3400. err, group);
  3401. return 0;
  3402. }
  3403. err = ext4_mb_load_buddy(sb, group, &e4b);
  3404. if (err) {
  3405. ext4_error(sb, "Error loading buddy information for %u", group);
  3406. put_bh(bitmap_bh);
  3407. return 0;
  3408. }
  3409. if (needed == 0)
  3410. needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1;
  3411. INIT_LIST_HEAD(&list);
  3412. repeat:
  3413. ext4_lock_group(sb, group);
  3414. list_for_each_entry_safe(pa, tmp,
  3415. &grp->bb_prealloc_list, pa_group_list) {
  3416. spin_lock(&pa->pa_lock);
  3417. if (atomic_read(&pa->pa_count)) {
  3418. spin_unlock(&pa->pa_lock);
  3419. busy = 1;
  3420. continue;
  3421. }
  3422. if (pa->pa_deleted) {
  3423. spin_unlock(&pa->pa_lock);
  3424. continue;
  3425. }
  3426. /* seems this one can be freed ... */
  3427. pa->pa_deleted = 1;
  3428. /* we can trust pa_free ... */
  3429. free += pa->pa_free;
  3430. spin_unlock(&pa->pa_lock);
  3431. list_del(&pa->pa_group_list);
  3432. list_add(&pa->u.pa_tmp_list, &list);
  3433. }
  3434. /* if we still need more blocks and some PAs were used, try again */
  3435. if (free < needed && busy) {
  3436. busy = 0;
  3437. ext4_unlock_group(sb, group);
  3438. cond_resched();
  3439. goto repeat;
  3440. }
  3441. /* found anything to free? */
  3442. if (list_empty(&list)) {
  3443. BUG_ON(free != 0);
  3444. goto out;
  3445. }
  3446. /* now free all selected PAs */
  3447. list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
  3448. /* remove from object (inode or locality group) */
  3449. spin_lock(pa->pa_obj_lock);
  3450. list_del_rcu(&pa->pa_inode_list);
  3451. spin_unlock(pa->pa_obj_lock);
  3452. if (pa->pa_type == MB_GROUP_PA)
  3453. ext4_mb_release_group_pa(&e4b, pa);
  3454. else
  3455. ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
  3456. list_del(&pa->u.pa_tmp_list);
  3457. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3458. }
  3459. out:
  3460. ext4_unlock_group(sb, group);
  3461. ext4_mb_unload_buddy(&e4b);
  3462. put_bh(bitmap_bh);
  3463. return free;
  3464. }
  3465. /*
  3466. * releases all non-used preallocated blocks for given inode
  3467. *
  3468. * It's important to discard preallocations under i_data_sem
  3469. * We don't want another block to be served from the prealloc
  3470. * space when we are discarding the inode prealloc space.
  3471. *
  3472. * FIXME!! Make sure it is valid at all the call sites
  3473. */
  3474. void ext4_discard_preallocations(struct inode *inode)
  3475. {
  3476. struct ext4_inode_info *ei = EXT4_I(inode);
  3477. struct super_block *sb = inode->i_sb;
  3478. struct buffer_head *bitmap_bh = NULL;
  3479. struct ext4_prealloc_space *pa, *tmp;
  3480. ext4_group_t group = 0;
  3481. struct list_head list;
  3482. struct ext4_buddy e4b;
  3483. int err;
  3484. if (!S_ISREG(inode->i_mode)) {
  3485. /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
  3486. return;
  3487. }
  3488. mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
  3489. trace_ext4_discard_preallocations(inode);
  3490. INIT_LIST_HEAD(&list);
  3491. repeat:
  3492. /* first, collect all pa's in the inode */
  3493. spin_lock(&ei->i_prealloc_lock);
  3494. while (!list_empty(&ei->i_prealloc_list)) {
  3495. pa = list_entry(ei->i_prealloc_list.next,
  3496. struct ext4_prealloc_space, pa_inode_list);
  3497. BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
  3498. spin_lock(&pa->pa_lock);
  3499. if (atomic_read(&pa->pa_count)) {
  3500. /* this shouldn't happen often - nobody should
  3501. * use preallocation while we're discarding it */
  3502. spin_unlock(&pa->pa_lock);
  3503. spin_unlock(&ei->i_prealloc_lock);
  3504. ext4_msg(sb, KERN_ERR,
  3505. "uh-oh! used pa while discarding");
  3506. WARN_ON(1);
  3507. schedule_timeout_uninterruptible(HZ);
  3508. goto repeat;
  3509. }
  3510. if (pa->pa_deleted == 0) {
  3511. pa->pa_deleted = 1;
  3512. spin_unlock(&pa->pa_lock);
  3513. list_del_rcu(&pa->pa_inode_list);
  3514. list_add(&pa->u.pa_tmp_list, &list);
  3515. continue;
  3516. }
  3517. /* someone is deleting pa right now */
  3518. spin_unlock(&pa->pa_lock);
  3519. spin_unlock(&ei->i_prealloc_lock);
  3520. /* we have to wait here because pa_deleted
  3521. * doesn't mean pa is already unlinked from
  3522. * the list. as we might be called from
  3523. * ->clear_inode() the inode will get freed
  3524. * and concurrent thread which is unlinking
  3525. * pa from inode's list may access already
  3526. * freed memory, bad-bad-bad */
  3527. /* XXX: if this happens too often, we can
  3528. * add a flag to force wait only in case
  3529. * of ->clear_inode(), but not in case of
  3530. * regular truncate */
  3531. schedule_timeout_uninterruptible(HZ);
  3532. goto repeat;
  3533. }
  3534. spin_unlock(&ei->i_prealloc_lock);
  3535. list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
  3536. BUG_ON(pa->pa_type != MB_INODE_PA);
  3537. group = ext4_get_group_number(sb, pa->pa_pstart);
  3538. err = ext4_mb_load_buddy(sb, group, &e4b);
  3539. if (err) {
  3540. ext4_error(sb, "Error loading buddy information for %u",
  3541. group);
  3542. continue;
  3543. }
  3544. bitmap_bh = ext4_read_block_bitmap(sb, group);
  3545. if (IS_ERR(bitmap_bh)) {
  3546. err = PTR_ERR(bitmap_bh);
  3547. ext4_error(sb, "Error %d reading block bitmap for %u",
  3548. err, group);
  3549. ext4_mb_unload_buddy(&e4b);
  3550. continue;
  3551. }
  3552. ext4_lock_group(sb, group);
  3553. list_del(&pa->pa_group_list);
  3554. ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
  3555. ext4_unlock_group(sb, group);
  3556. ext4_mb_unload_buddy(&e4b);
  3557. put_bh(bitmap_bh);
  3558. list_del(&pa->u.pa_tmp_list);
  3559. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3560. }
  3561. }
  3562. #ifdef CONFIG_EXT4_DEBUG
  3563. static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
  3564. {
  3565. struct super_block *sb = ac->ac_sb;
  3566. ext4_group_t ngroups, i;
  3567. if (!ext4_mballoc_debug ||
  3568. (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED))
  3569. return;
  3570. ext4_msg(ac->ac_sb, KERN_ERR, "Can't allocate:"
  3571. " Allocation context details:");
  3572. ext4_msg(ac->ac_sb, KERN_ERR, "status %d flags %d",
  3573. ac->ac_status, ac->ac_flags);
  3574. ext4_msg(ac->ac_sb, KERN_ERR, "orig %lu/%lu/%lu@%lu, "
  3575. "goal %lu/%lu/%lu@%lu, "
  3576. "best %lu/%lu/%lu@%lu cr %d",
  3577. (unsigned long)ac->ac_o_ex.fe_group,
  3578. (unsigned long)ac->ac_o_ex.fe_start,
  3579. (unsigned long)ac->ac_o_ex.fe_len,
  3580. (unsigned long)ac->ac_o_ex.fe_logical,
  3581. (unsigned long)ac->ac_g_ex.fe_group,
  3582. (unsigned long)ac->ac_g_ex.fe_start,
  3583. (unsigned long)ac->ac_g_ex.fe_len,
  3584. (unsigned long)ac->ac_g_ex.fe_logical,
  3585. (unsigned long)ac->ac_b_ex.fe_group,
  3586. (unsigned long)ac->ac_b_ex.fe_start,
  3587. (unsigned long)ac->ac_b_ex.fe_len,
  3588. (unsigned long)ac->ac_b_ex.fe_logical,
  3589. (int)ac->ac_criteria);
  3590. ext4_msg(ac->ac_sb, KERN_ERR, "%d found", ac->ac_found);
  3591. ext4_msg(ac->ac_sb, KERN_ERR, "groups: ");
  3592. ngroups = ext4_get_groups_count(sb);
  3593. for (i = 0; i < ngroups; i++) {
  3594. struct ext4_group_info *grp = ext4_get_group_info(sb, i);
  3595. struct ext4_prealloc_space *pa;
  3596. ext4_grpblk_t start;
  3597. struct list_head *cur;
  3598. ext4_lock_group(sb, i);
  3599. list_for_each(cur, &grp->bb_prealloc_list) {
  3600. pa = list_entry(cur, struct ext4_prealloc_space,
  3601. pa_group_list);
  3602. spin_lock(&pa->pa_lock);
  3603. ext4_get_group_no_and_offset(sb, pa->pa_pstart,
  3604. NULL, &start);
  3605. spin_unlock(&pa->pa_lock);
  3606. printk(KERN_ERR "PA:%u:%d:%u \n", i,
  3607. start, pa->pa_len);
  3608. }
  3609. ext4_unlock_group(sb, i);
  3610. if (grp->bb_free == 0)
  3611. continue;
  3612. printk(KERN_ERR "%u: %d/%d \n",
  3613. i, grp->bb_free, grp->bb_fragments);
  3614. }
  3615. printk(KERN_ERR "\n");
  3616. }
  3617. #else
  3618. static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
  3619. {
  3620. return;
  3621. }
  3622. #endif
  3623. /*
  3624. * We use locality group preallocation for small size file. The size of the
  3625. * file is determined by the current size or the resulting size after
  3626. * allocation which ever is larger
  3627. *
  3628. * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
  3629. */
  3630. static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
  3631. {
  3632. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  3633. int bsbits = ac->ac_sb->s_blocksize_bits;
  3634. loff_t size, isize;
  3635. if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
  3636. return;
  3637. if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
  3638. return;
  3639. size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
  3640. isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
  3641. >> bsbits;
  3642. if ((size == isize) &&
  3643. !ext4_fs_is_busy(sbi) &&
  3644. (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
  3645. ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
  3646. return;
  3647. }
  3648. if (sbi->s_mb_group_prealloc <= 0) {
  3649. ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
  3650. return;
  3651. }
  3652. /* don't use group allocation for large files */
  3653. size = max(size, isize);
  3654. if (size > sbi->s_mb_stream_request) {
  3655. ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
  3656. return;
  3657. }
  3658. BUG_ON(ac->ac_lg != NULL);
  3659. /*
  3660. * locality group prealloc space are per cpu. The reason for having
  3661. * per cpu locality group is to reduce the contention between block
  3662. * request from multiple CPUs.
  3663. */
  3664. ac->ac_lg = raw_cpu_ptr(sbi->s_locality_groups);
  3665. /* we're going to use group allocation */
  3666. ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
  3667. /* serialize all allocations in the group */
  3668. mutex_lock(&ac->ac_lg->lg_mutex);
  3669. }
  3670. static noinline_for_stack int
  3671. ext4_mb_initialize_context(struct ext4_allocation_context *ac,
  3672. struct ext4_allocation_request *ar)
  3673. {
  3674. struct super_block *sb = ar->inode->i_sb;
  3675. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3676. struct ext4_super_block *es = sbi->s_es;
  3677. ext4_group_t group;
  3678. unsigned int len;
  3679. ext4_fsblk_t goal;
  3680. ext4_grpblk_t block;
  3681. /* we can't allocate > group size */
  3682. len = ar->len;
  3683. /* just a dirty hack to filter too big requests */
  3684. if (len >= EXT4_CLUSTERS_PER_GROUP(sb))
  3685. len = EXT4_CLUSTERS_PER_GROUP(sb);
  3686. /* start searching from the goal */
  3687. goal = ar->goal;
  3688. if (goal < le32_to_cpu(es->s_first_data_block) ||
  3689. goal >= ext4_blocks_count(es))
  3690. goal = le32_to_cpu(es->s_first_data_block);
  3691. ext4_get_group_no_and_offset(sb, goal, &group, &block);
  3692. /* set up allocation goals */
  3693. ac->ac_b_ex.fe_logical = EXT4_LBLK_CMASK(sbi, ar->logical);
  3694. ac->ac_status = AC_STATUS_CONTINUE;
  3695. ac->ac_sb = sb;
  3696. ac->ac_inode = ar->inode;
  3697. ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical;
  3698. ac->ac_o_ex.fe_group = group;
  3699. ac->ac_o_ex.fe_start = block;
  3700. ac->ac_o_ex.fe_len = len;
  3701. ac->ac_g_ex = ac->ac_o_ex;
  3702. ac->ac_flags = ar->flags;
  3703. /* we have to define context: we'll we work with a file or
  3704. * locality group. this is a policy, actually */
  3705. ext4_mb_group_or_file(ac);
  3706. mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
  3707. "left: %u/%u, right %u/%u to %swritable\n",
  3708. (unsigned) ar->len, (unsigned) ar->logical,
  3709. (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
  3710. (unsigned) ar->lleft, (unsigned) ar->pleft,
  3711. (unsigned) ar->lright, (unsigned) ar->pright,
  3712. atomic_read(&ar->inode->i_writecount) ? "" : "non-");
  3713. return 0;
  3714. }
  3715. static noinline_for_stack void
  3716. ext4_mb_discard_lg_preallocations(struct super_block *sb,
  3717. struct ext4_locality_group *lg,
  3718. int order, int total_entries)
  3719. {
  3720. ext4_group_t group = 0;
  3721. struct ext4_buddy e4b;
  3722. struct list_head discard_list;
  3723. struct ext4_prealloc_space *pa, *tmp;
  3724. mb_debug(1, "discard locality group preallocation\n");
  3725. INIT_LIST_HEAD(&discard_list);
  3726. spin_lock(&lg->lg_prealloc_lock);
  3727. list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
  3728. pa_inode_list) {
  3729. spin_lock(&pa->pa_lock);
  3730. if (atomic_read(&pa->pa_count)) {
  3731. /*
  3732. * This is the pa that we just used
  3733. * for block allocation. So don't
  3734. * free that
  3735. */
  3736. spin_unlock(&pa->pa_lock);
  3737. continue;
  3738. }
  3739. if (pa->pa_deleted) {
  3740. spin_unlock(&pa->pa_lock);
  3741. continue;
  3742. }
  3743. /* only lg prealloc space */
  3744. BUG_ON(pa->pa_type != MB_GROUP_PA);
  3745. /* seems this one can be freed ... */
  3746. pa->pa_deleted = 1;
  3747. spin_unlock(&pa->pa_lock);
  3748. list_del_rcu(&pa->pa_inode_list);
  3749. list_add(&pa->u.pa_tmp_list, &discard_list);
  3750. total_entries--;
  3751. if (total_entries <= 5) {
  3752. /*
  3753. * we want to keep only 5 entries
  3754. * allowing it to grow to 8. This
  3755. * mak sure we don't call discard
  3756. * soon for this list.
  3757. */
  3758. break;
  3759. }
  3760. }
  3761. spin_unlock(&lg->lg_prealloc_lock);
  3762. list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
  3763. group = ext4_get_group_number(sb, pa->pa_pstart);
  3764. if (ext4_mb_load_buddy(sb, group, &e4b)) {
  3765. ext4_error(sb, "Error loading buddy information for %u",
  3766. group);
  3767. continue;
  3768. }
  3769. ext4_lock_group(sb, group);
  3770. list_del(&pa->pa_group_list);
  3771. ext4_mb_release_group_pa(&e4b, pa);
  3772. ext4_unlock_group(sb, group);
  3773. ext4_mb_unload_buddy(&e4b);
  3774. list_del(&pa->u.pa_tmp_list);
  3775. call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
  3776. }
  3777. }
  3778. /*
  3779. * We have incremented pa_count. So it cannot be freed at this
  3780. * point. Also we hold lg_mutex. So no parallel allocation is
  3781. * possible from this lg. That means pa_free cannot be updated.
  3782. *
  3783. * A parallel ext4_mb_discard_group_preallocations is possible.
  3784. * which can cause the lg_prealloc_list to be updated.
  3785. */
  3786. static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
  3787. {
  3788. int order, added = 0, lg_prealloc_count = 1;
  3789. struct super_block *sb = ac->ac_sb;
  3790. struct ext4_locality_group *lg = ac->ac_lg;
  3791. struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
  3792. order = fls(pa->pa_free) - 1;
  3793. if (order > PREALLOC_TB_SIZE - 1)
  3794. /* The max size of hash table is PREALLOC_TB_SIZE */
  3795. order = PREALLOC_TB_SIZE - 1;
  3796. /* Add the prealloc space to lg */
  3797. spin_lock(&lg->lg_prealloc_lock);
  3798. list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
  3799. pa_inode_list) {
  3800. spin_lock(&tmp_pa->pa_lock);
  3801. if (tmp_pa->pa_deleted) {
  3802. spin_unlock(&tmp_pa->pa_lock);
  3803. continue;
  3804. }
  3805. if (!added && pa->pa_free < tmp_pa->pa_free) {
  3806. /* Add to the tail of the previous entry */
  3807. list_add_tail_rcu(&pa->pa_inode_list,
  3808. &tmp_pa->pa_inode_list);
  3809. added = 1;
  3810. /*
  3811. * we want to count the total
  3812. * number of entries in the list
  3813. */
  3814. }
  3815. spin_unlock(&tmp_pa->pa_lock);
  3816. lg_prealloc_count++;
  3817. }
  3818. if (!added)
  3819. list_add_tail_rcu(&pa->pa_inode_list,
  3820. &lg->lg_prealloc_list[order]);
  3821. spin_unlock(&lg->lg_prealloc_lock);
  3822. /* Now trim the list to be not more than 8 elements */
  3823. if (lg_prealloc_count > 8) {
  3824. ext4_mb_discard_lg_preallocations(sb, lg,
  3825. order, lg_prealloc_count);
  3826. return;
  3827. }
  3828. return ;
  3829. }
  3830. /*
  3831. * release all resource we used in allocation
  3832. */
  3833. static int ext4_mb_release_context(struct ext4_allocation_context *ac)
  3834. {
  3835. struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
  3836. struct ext4_prealloc_space *pa = ac->ac_pa;
  3837. if (pa) {
  3838. if (pa->pa_type == MB_GROUP_PA) {
  3839. /* see comment in ext4_mb_use_group_pa() */
  3840. spin_lock(&pa->pa_lock);
  3841. pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
  3842. pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
  3843. pa->pa_free -= ac->ac_b_ex.fe_len;
  3844. pa->pa_len -= ac->ac_b_ex.fe_len;
  3845. spin_unlock(&pa->pa_lock);
  3846. }
  3847. }
  3848. if (pa) {
  3849. /*
  3850. * We want to add the pa to the right bucket.
  3851. * Remove it from the list and while adding
  3852. * make sure the list to which we are adding
  3853. * doesn't grow big.
  3854. */
  3855. if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
  3856. spin_lock(pa->pa_obj_lock);
  3857. list_del_rcu(&pa->pa_inode_list);
  3858. spin_unlock(pa->pa_obj_lock);
  3859. ext4_mb_add_n_trim(ac);
  3860. }
  3861. ext4_mb_put_pa(ac, ac->ac_sb, pa);
  3862. }
  3863. if (ac->ac_bitmap_page)
  3864. put_page(ac->ac_bitmap_page);
  3865. if (ac->ac_buddy_page)
  3866. put_page(ac->ac_buddy_page);
  3867. if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
  3868. mutex_unlock(&ac->ac_lg->lg_mutex);
  3869. ext4_mb_collect_stats(ac);
  3870. return 0;
  3871. }
  3872. static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
  3873. {
  3874. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  3875. int ret;
  3876. int freed = 0;
  3877. trace_ext4_mb_discard_preallocations(sb, needed);
  3878. for (i = 0; i < ngroups && needed > 0; i++) {
  3879. ret = ext4_mb_discard_group_preallocations(sb, i, needed);
  3880. freed += ret;
  3881. needed -= ret;
  3882. }
  3883. return freed;
  3884. }
  3885. /*
  3886. * Main entry point into mballoc to allocate blocks
  3887. * it tries to use preallocation first, then falls back
  3888. * to usual allocation
  3889. */
  3890. ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
  3891. struct ext4_allocation_request *ar, int *errp)
  3892. {
  3893. int freed;
  3894. struct ext4_allocation_context *ac = NULL;
  3895. struct ext4_sb_info *sbi;
  3896. struct super_block *sb;
  3897. ext4_fsblk_t block = 0;
  3898. unsigned int inquota = 0;
  3899. unsigned int reserv_clstrs = 0;
  3900. might_sleep();
  3901. sb = ar->inode->i_sb;
  3902. sbi = EXT4_SB(sb);
  3903. trace_ext4_request_blocks(ar);
  3904. /* Allow to use superuser reservation for quota file */
  3905. if (IS_NOQUOTA(ar->inode))
  3906. ar->flags |= EXT4_MB_USE_ROOT_BLOCKS;
  3907. if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) {
  3908. /* Without delayed allocation we need to verify
  3909. * there is enough free blocks to do block allocation
  3910. * and verify allocation doesn't exceed the quota limits.
  3911. */
  3912. while (ar->len &&
  3913. ext4_claim_free_clusters(sbi, ar->len, ar->flags)) {
  3914. /* let others to free the space */
  3915. cond_resched();
  3916. ar->len = ar->len >> 1;
  3917. }
  3918. if (!ar->len) {
  3919. *errp = -ENOSPC;
  3920. return 0;
  3921. }
  3922. reserv_clstrs = ar->len;
  3923. if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) {
  3924. dquot_alloc_block_nofail(ar->inode,
  3925. EXT4_C2B(sbi, ar->len));
  3926. } else {
  3927. while (ar->len &&
  3928. dquot_alloc_block(ar->inode,
  3929. EXT4_C2B(sbi, ar->len))) {
  3930. ar->flags |= EXT4_MB_HINT_NOPREALLOC;
  3931. ar->len--;
  3932. }
  3933. }
  3934. inquota = ar->len;
  3935. if (ar->len == 0) {
  3936. *errp = -EDQUOT;
  3937. goto out;
  3938. }
  3939. }
  3940. ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS);
  3941. if (!ac) {
  3942. ar->len = 0;
  3943. *errp = -ENOMEM;
  3944. goto out;
  3945. }
  3946. *errp = ext4_mb_initialize_context(ac, ar);
  3947. if (*errp) {
  3948. ar->len = 0;
  3949. goto out;
  3950. }
  3951. ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
  3952. if (!ext4_mb_use_preallocated(ac)) {
  3953. ac->ac_op = EXT4_MB_HISTORY_ALLOC;
  3954. ext4_mb_normalize_request(ac, ar);
  3955. repeat:
  3956. /* allocate space in core */
  3957. *errp = ext4_mb_regular_allocator(ac);
  3958. if (*errp)
  3959. goto discard_and_exit;
  3960. /* as we've just preallocated more space than
  3961. * user requested originally, we store allocated
  3962. * space in a special descriptor */
  3963. if (ac->ac_status == AC_STATUS_FOUND &&
  3964. ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
  3965. *errp = ext4_mb_new_preallocation(ac);
  3966. if (*errp) {
  3967. discard_and_exit:
  3968. ext4_discard_allocated_blocks(ac);
  3969. goto errout;
  3970. }
  3971. }
  3972. if (likely(ac->ac_status == AC_STATUS_FOUND)) {
  3973. *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
  3974. if (*errp == -EAGAIN) {
  3975. /*
  3976. * drop the reference that we took
  3977. * in ext4_mb_use_best_found
  3978. */
  3979. ext4_mb_release_context(ac);
  3980. ac->ac_b_ex.fe_group = 0;
  3981. ac->ac_b_ex.fe_start = 0;
  3982. ac->ac_b_ex.fe_len = 0;
  3983. ac->ac_status = AC_STATUS_CONTINUE;
  3984. goto repeat;
  3985. } else if (*errp) {
  3986. ext4_discard_allocated_blocks(ac);
  3987. goto errout;
  3988. } else {
  3989. block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
  3990. ar->len = ac->ac_b_ex.fe_len;
  3991. }
  3992. } else {
  3993. freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
  3994. if (freed)
  3995. goto repeat;
  3996. *errp = -ENOSPC;
  3997. }
  3998. errout:
  3999. if (*errp) {
  4000. ac->ac_b_ex.fe_len = 0;
  4001. ar->len = 0;
  4002. ext4_mb_show_ac(ac);
  4003. }
  4004. ext4_mb_release_context(ac);
  4005. out:
  4006. if (ac)
  4007. kmem_cache_free(ext4_ac_cachep, ac);
  4008. if (inquota && ar->len < inquota)
  4009. dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len));
  4010. if (!ar->len) {
  4011. if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0)
  4012. /* release all the reserved blocks if non delalloc */
  4013. percpu_counter_sub(&sbi->s_dirtyclusters_counter,
  4014. reserv_clstrs);
  4015. }
  4016. trace_ext4_allocate_blocks(ar, (unsigned long long)block);
  4017. return block;
  4018. }
  4019. /*
  4020. * We can merge two free data extents only if the physical blocks
  4021. * are contiguous, AND the extents were freed by the same transaction,
  4022. * AND the blocks are associated with the same group.
  4023. */
  4024. static int can_merge(struct ext4_free_data *entry1,
  4025. struct ext4_free_data *entry2)
  4026. {
  4027. if ((entry1->efd_tid == entry2->efd_tid) &&
  4028. (entry1->efd_group == entry2->efd_group) &&
  4029. ((entry1->efd_start_cluster + entry1->efd_count) == entry2->efd_start_cluster))
  4030. return 1;
  4031. return 0;
  4032. }
  4033. static noinline_for_stack int
  4034. ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
  4035. struct ext4_free_data *new_entry)
  4036. {
  4037. ext4_group_t group = e4b->bd_group;
  4038. ext4_grpblk_t cluster;
  4039. struct ext4_free_data *entry;
  4040. struct ext4_group_info *db = e4b->bd_info;
  4041. struct super_block *sb = e4b->bd_sb;
  4042. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4043. struct rb_node **n = &db->bb_free_root.rb_node, *node;
  4044. struct rb_node *parent = NULL, *new_node;
  4045. BUG_ON(!ext4_handle_valid(handle));
  4046. BUG_ON(e4b->bd_bitmap_page == NULL);
  4047. BUG_ON(e4b->bd_buddy_page == NULL);
  4048. new_node = &new_entry->efd_node;
  4049. cluster = new_entry->efd_start_cluster;
  4050. if (!*n) {
  4051. /* first free block exent. We need to
  4052. protect buddy cache from being freed,
  4053. * otherwise we'll refresh it from
  4054. * on-disk bitmap and lose not-yet-available
  4055. * blocks */
  4056. get_page(e4b->bd_buddy_page);
  4057. get_page(e4b->bd_bitmap_page);
  4058. }
  4059. while (*n) {
  4060. parent = *n;
  4061. entry = rb_entry(parent, struct ext4_free_data, efd_node);
  4062. if (cluster < entry->efd_start_cluster)
  4063. n = &(*n)->rb_left;
  4064. else if (cluster >= (entry->efd_start_cluster + entry->efd_count))
  4065. n = &(*n)->rb_right;
  4066. else {
  4067. ext4_grp_locked_error(sb, group, 0,
  4068. ext4_group_first_block_no(sb, group) +
  4069. EXT4_C2B(sbi, cluster),
  4070. "Block already on to-be-freed list");
  4071. return 0;
  4072. }
  4073. }
  4074. rb_link_node(new_node, parent, n);
  4075. rb_insert_color(new_node, &db->bb_free_root);
  4076. /* Now try to see the extent can be merged to left and right */
  4077. node = rb_prev(new_node);
  4078. if (node) {
  4079. entry = rb_entry(node, struct ext4_free_data, efd_node);
  4080. if (can_merge(entry, new_entry) &&
  4081. ext4_journal_callback_try_del(handle, &entry->efd_jce)) {
  4082. new_entry->efd_start_cluster = entry->efd_start_cluster;
  4083. new_entry->efd_count += entry->efd_count;
  4084. rb_erase(node, &(db->bb_free_root));
  4085. kmem_cache_free(ext4_free_data_cachep, entry);
  4086. }
  4087. }
  4088. node = rb_next(new_node);
  4089. if (node) {
  4090. entry = rb_entry(node, struct ext4_free_data, efd_node);
  4091. if (can_merge(new_entry, entry) &&
  4092. ext4_journal_callback_try_del(handle, &entry->efd_jce)) {
  4093. new_entry->efd_count += entry->efd_count;
  4094. rb_erase(node, &(db->bb_free_root));
  4095. kmem_cache_free(ext4_free_data_cachep, entry);
  4096. }
  4097. }
  4098. /* Add the extent to transaction's private list */
  4099. ext4_journal_callback_add(handle, ext4_free_data_callback,
  4100. &new_entry->efd_jce);
  4101. return 0;
  4102. }
  4103. /**
  4104. * ext4_free_blocks() -- Free given blocks and update quota
  4105. * @handle: handle for this transaction
  4106. * @inode: inode
  4107. * @block: start physical block to free
  4108. * @count: number of blocks to count
  4109. * @flags: flags used by ext4_free_blocks
  4110. */
  4111. void ext4_free_blocks(handle_t *handle, struct inode *inode,
  4112. struct buffer_head *bh, ext4_fsblk_t block,
  4113. unsigned long count, int flags)
  4114. {
  4115. struct buffer_head *bitmap_bh = NULL;
  4116. struct super_block *sb = inode->i_sb;
  4117. struct ext4_group_desc *gdp;
  4118. unsigned int overflow;
  4119. ext4_grpblk_t bit;
  4120. struct buffer_head *gd_bh;
  4121. ext4_group_t block_group;
  4122. struct ext4_sb_info *sbi;
  4123. struct ext4_buddy e4b;
  4124. unsigned int count_clusters;
  4125. int err = 0;
  4126. int ret;
  4127. might_sleep();
  4128. if (bh) {
  4129. if (block)
  4130. BUG_ON(block != bh->b_blocknr);
  4131. else
  4132. block = bh->b_blocknr;
  4133. }
  4134. sbi = EXT4_SB(sb);
  4135. if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
  4136. !ext4_data_block_valid(sbi, block, count)) {
  4137. ext4_error(sb, "Freeing blocks not in datazone - "
  4138. "block = %llu, count = %lu", block, count);
  4139. goto error_return;
  4140. }
  4141. ext4_debug("freeing block %llu\n", block);
  4142. trace_ext4_free_blocks(inode, block, count, flags);
  4143. if (bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
  4144. BUG_ON(count > 1);
  4145. ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
  4146. inode, bh, block);
  4147. }
  4148. /*
  4149. * If the extent to be freed does not begin on a cluster
  4150. * boundary, we need to deal with partial clusters at the
  4151. * beginning and end of the extent. Normally we will free
  4152. * blocks at the beginning or the end unless we are explicitly
  4153. * requested to avoid doing so.
  4154. */
  4155. overflow = EXT4_PBLK_COFF(sbi, block);
  4156. if (overflow) {
  4157. if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) {
  4158. overflow = sbi->s_cluster_ratio - overflow;
  4159. block += overflow;
  4160. if (count > overflow)
  4161. count -= overflow;
  4162. else
  4163. return;
  4164. } else {
  4165. block -= overflow;
  4166. count += overflow;
  4167. }
  4168. }
  4169. overflow = EXT4_LBLK_COFF(sbi, count);
  4170. if (overflow) {
  4171. if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) {
  4172. if (count > overflow)
  4173. count -= overflow;
  4174. else
  4175. return;
  4176. } else
  4177. count += sbi->s_cluster_ratio - overflow;
  4178. }
  4179. if (!bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
  4180. int i;
  4181. int is_metadata = flags & EXT4_FREE_BLOCKS_METADATA;
  4182. for (i = 0; i < count; i++) {
  4183. cond_resched();
  4184. if (is_metadata)
  4185. bh = sb_find_get_block(inode->i_sb, block + i);
  4186. ext4_forget(handle, is_metadata, inode, bh, block + i);
  4187. }
  4188. }
  4189. do_more:
  4190. overflow = 0;
  4191. ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
  4192. if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(
  4193. ext4_get_group_info(sb, block_group))))
  4194. return;
  4195. /*
  4196. * Check to see if we are freeing blocks across a group
  4197. * boundary.
  4198. */
  4199. if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) {
  4200. overflow = EXT4_C2B(sbi, bit) + count -
  4201. EXT4_BLOCKS_PER_GROUP(sb);
  4202. count -= overflow;
  4203. }
  4204. count_clusters = EXT4_NUM_B2C(sbi, count);
  4205. bitmap_bh = ext4_read_block_bitmap(sb, block_group);
  4206. if (IS_ERR(bitmap_bh)) {
  4207. err = PTR_ERR(bitmap_bh);
  4208. bitmap_bh = NULL;
  4209. goto error_return;
  4210. }
  4211. gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
  4212. if (!gdp) {
  4213. err = -EIO;
  4214. goto error_return;
  4215. }
  4216. if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
  4217. in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
  4218. in_range(block, ext4_inode_table(sb, gdp),
  4219. EXT4_SB(sb)->s_itb_per_group) ||
  4220. in_range(block + count - 1, ext4_inode_table(sb, gdp),
  4221. EXT4_SB(sb)->s_itb_per_group)) {
  4222. ext4_error(sb, "Freeing blocks in system zone - "
  4223. "Block = %llu, count = %lu", block, count);
  4224. /* err = 0. ext4_std_error should be a no op */
  4225. goto error_return;
  4226. }
  4227. BUFFER_TRACE(bitmap_bh, "getting write access");
  4228. err = ext4_journal_get_write_access(handle, bitmap_bh);
  4229. if (err)
  4230. goto error_return;
  4231. /*
  4232. * We are about to modify some metadata. Call the journal APIs
  4233. * to unshare ->b_data if a currently-committing transaction is
  4234. * using it
  4235. */
  4236. BUFFER_TRACE(gd_bh, "get_write_access");
  4237. err = ext4_journal_get_write_access(handle, gd_bh);
  4238. if (err)
  4239. goto error_return;
  4240. #ifdef AGGRESSIVE_CHECK
  4241. {
  4242. int i;
  4243. for (i = 0; i < count_clusters; i++)
  4244. BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
  4245. }
  4246. #endif
  4247. trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters);
  4248. /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */
  4249. err = ext4_mb_load_buddy_gfp(sb, block_group, &e4b,
  4250. GFP_NOFS|__GFP_NOFAIL);
  4251. if (err)
  4252. goto error_return;
  4253. /*
  4254. * We need to make sure we don't reuse the freed block until after the
  4255. * transaction is committed. We make an exception if the inode is to be
  4256. * written in writeback mode since writeback mode has weak data
  4257. * consistency guarantees.
  4258. */
  4259. if (ext4_handle_valid(handle) &&
  4260. ((flags & EXT4_FREE_BLOCKS_METADATA) ||
  4261. !ext4_should_writeback_data(inode))) {
  4262. struct ext4_free_data *new_entry;
  4263. /*
  4264. * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed
  4265. * to fail.
  4266. */
  4267. new_entry = kmem_cache_alloc(ext4_free_data_cachep,
  4268. GFP_NOFS|__GFP_NOFAIL);
  4269. new_entry->efd_start_cluster = bit;
  4270. new_entry->efd_group = block_group;
  4271. new_entry->efd_count = count_clusters;
  4272. new_entry->efd_tid = handle->h_transaction->t_tid;
  4273. ext4_lock_group(sb, block_group);
  4274. mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
  4275. ext4_mb_free_metadata(handle, &e4b, new_entry);
  4276. } else {
  4277. /* need to update group_info->bb_free and bitmap
  4278. * with group lock held. generate_buddy look at
  4279. * them with group lock_held
  4280. */
  4281. if (test_opt(sb, DISCARD)) {
  4282. err = ext4_issue_discard(sb, block_group, bit, count);
  4283. if (err && err != -EOPNOTSUPP)
  4284. ext4_msg(sb, KERN_WARNING, "discard request in"
  4285. " group:%d block:%d count:%lu failed"
  4286. " with %d", block_group, bit, count,
  4287. err);
  4288. } else
  4289. EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info);
  4290. ext4_lock_group(sb, block_group);
  4291. mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
  4292. mb_free_blocks(inode, &e4b, bit, count_clusters);
  4293. }
  4294. ret = ext4_free_group_clusters(sb, gdp) + count_clusters;
  4295. ext4_free_group_clusters_set(sb, gdp, ret);
  4296. ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh);
  4297. ext4_group_desc_csum_set(sb, block_group, gdp);
  4298. ext4_unlock_group(sb, block_group);
  4299. if (sbi->s_log_groups_per_flex) {
  4300. ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
  4301. atomic64_add(count_clusters,
  4302. &sbi->s_flex_groups[flex_group].free_clusters);
  4303. }
  4304. if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE))
  4305. dquot_free_block(inode, EXT4_C2B(sbi, count_clusters));
  4306. percpu_counter_add(&sbi->s_freeclusters_counter, count_clusters);
  4307. ext4_mb_unload_buddy(&e4b);
  4308. /* We dirtied the bitmap block */
  4309. BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
  4310. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  4311. /* And the group descriptor block */
  4312. BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
  4313. ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
  4314. if (!err)
  4315. err = ret;
  4316. if (overflow && !err) {
  4317. block += count;
  4318. count = overflow;
  4319. put_bh(bitmap_bh);
  4320. goto do_more;
  4321. }
  4322. error_return:
  4323. brelse(bitmap_bh);
  4324. ext4_std_error(sb, err);
  4325. return;
  4326. }
  4327. /**
  4328. * ext4_group_add_blocks() -- Add given blocks to an existing group
  4329. * @handle: handle to this transaction
  4330. * @sb: super block
  4331. * @block: start physical block to add to the block group
  4332. * @count: number of blocks to free
  4333. *
  4334. * This marks the blocks as free in the bitmap and buddy.
  4335. */
  4336. int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
  4337. ext4_fsblk_t block, unsigned long count)
  4338. {
  4339. struct buffer_head *bitmap_bh = NULL;
  4340. struct buffer_head *gd_bh;
  4341. ext4_group_t block_group;
  4342. ext4_grpblk_t bit;
  4343. unsigned int i;
  4344. struct ext4_group_desc *desc;
  4345. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4346. struct ext4_buddy e4b;
  4347. int err = 0, ret, blk_free_count;
  4348. ext4_grpblk_t blocks_freed;
  4349. ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
  4350. if (count == 0)
  4351. return 0;
  4352. ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
  4353. /*
  4354. * Check to see if we are freeing blocks across a group
  4355. * boundary.
  4356. */
  4357. if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
  4358. ext4_warning(sb, "too much blocks added to group %u",
  4359. block_group);
  4360. err = -EINVAL;
  4361. goto error_return;
  4362. }
  4363. bitmap_bh = ext4_read_block_bitmap(sb, block_group);
  4364. if (IS_ERR(bitmap_bh)) {
  4365. err = PTR_ERR(bitmap_bh);
  4366. bitmap_bh = NULL;
  4367. goto error_return;
  4368. }
  4369. desc = ext4_get_group_desc(sb, block_group, &gd_bh);
  4370. if (!desc) {
  4371. err = -EIO;
  4372. goto error_return;
  4373. }
  4374. if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
  4375. in_range(ext4_inode_bitmap(sb, desc), block, count) ||
  4376. in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
  4377. in_range(block + count - 1, ext4_inode_table(sb, desc),
  4378. sbi->s_itb_per_group)) {
  4379. ext4_error(sb, "Adding blocks in system zones - "
  4380. "Block = %llu, count = %lu",
  4381. block, count);
  4382. err = -EINVAL;
  4383. goto error_return;
  4384. }
  4385. BUFFER_TRACE(bitmap_bh, "getting write access");
  4386. err = ext4_journal_get_write_access(handle, bitmap_bh);
  4387. if (err)
  4388. goto error_return;
  4389. /*
  4390. * We are about to modify some metadata. Call the journal APIs
  4391. * to unshare ->b_data if a currently-committing transaction is
  4392. * using it
  4393. */
  4394. BUFFER_TRACE(gd_bh, "get_write_access");
  4395. err = ext4_journal_get_write_access(handle, gd_bh);
  4396. if (err)
  4397. goto error_return;
  4398. for (i = 0, blocks_freed = 0; i < count; i++) {
  4399. BUFFER_TRACE(bitmap_bh, "clear bit");
  4400. if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
  4401. ext4_error(sb, "bit already cleared for block %llu",
  4402. (ext4_fsblk_t)(block + i));
  4403. BUFFER_TRACE(bitmap_bh, "bit already cleared");
  4404. } else {
  4405. blocks_freed++;
  4406. }
  4407. }
  4408. err = ext4_mb_load_buddy(sb, block_group, &e4b);
  4409. if (err)
  4410. goto error_return;
  4411. /*
  4412. * need to update group_info->bb_free and bitmap
  4413. * with group lock held. generate_buddy look at
  4414. * them with group lock_held
  4415. */
  4416. ext4_lock_group(sb, block_group);
  4417. mb_clear_bits(bitmap_bh->b_data, bit, count);
  4418. mb_free_blocks(NULL, &e4b, bit, count);
  4419. blk_free_count = blocks_freed + ext4_free_group_clusters(sb, desc);
  4420. ext4_free_group_clusters_set(sb, desc, blk_free_count);
  4421. ext4_block_bitmap_csum_set(sb, block_group, desc, bitmap_bh);
  4422. ext4_group_desc_csum_set(sb, block_group, desc);
  4423. ext4_unlock_group(sb, block_group);
  4424. percpu_counter_add(&sbi->s_freeclusters_counter,
  4425. EXT4_NUM_B2C(sbi, blocks_freed));
  4426. if (sbi->s_log_groups_per_flex) {
  4427. ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
  4428. atomic64_add(EXT4_NUM_B2C(sbi, blocks_freed),
  4429. &sbi->s_flex_groups[flex_group].free_clusters);
  4430. }
  4431. ext4_mb_unload_buddy(&e4b);
  4432. /* We dirtied the bitmap block */
  4433. BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
  4434. err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
  4435. /* And the group descriptor block */
  4436. BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
  4437. ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
  4438. if (!err)
  4439. err = ret;
  4440. error_return:
  4441. brelse(bitmap_bh);
  4442. ext4_std_error(sb, err);
  4443. return err;
  4444. }
  4445. /**
  4446. * ext4_trim_extent -- function to TRIM one single free extent in the group
  4447. * @sb: super block for the file system
  4448. * @start: starting block of the free extent in the alloc. group
  4449. * @count: number of blocks to TRIM
  4450. * @group: alloc. group we are working with
  4451. * @e4b: ext4 buddy for the group
  4452. *
  4453. * Trim "count" blocks starting at "start" in the "group". To assure that no
  4454. * one will allocate those blocks, mark it as used in buddy bitmap. This must
  4455. * be called with under the group lock.
  4456. */
  4457. static int ext4_trim_extent(struct super_block *sb, int start, int count,
  4458. ext4_group_t group, struct ext4_buddy *e4b)
  4459. __releases(bitlock)
  4460. __acquires(bitlock)
  4461. {
  4462. struct ext4_free_extent ex;
  4463. int ret = 0;
  4464. trace_ext4_trim_extent(sb, group, start, count);
  4465. assert_spin_locked(ext4_group_lock_ptr(sb, group));
  4466. ex.fe_start = start;
  4467. ex.fe_group = group;
  4468. ex.fe_len = count;
  4469. /*
  4470. * Mark blocks used, so no one can reuse them while
  4471. * being trimmed.
  4472. */
  4473. mb_mark_used(e4b, &ex);
  4474. ext4_unlock_group(sb, group);
  4475. ret = ext4_issue_discard(sb, group, start, count);
  4476. ext4_lock_group(sb, group);
  4477. mb_free_blocks(NULL, e4b, start, ex.fe_len);
  4478. return ret;
  4479. }
  4480. /**
  4481. * ext4_trim_all_free -- function to trim all free space in alloc. group
  4482. * @sb: super block for file system
  4483. * @group: group to be trimmed
  4484. * @start: first group block to examine
  4485. * @max: last group block to examine
  4486. * @minblocks: minimum extent block count
  4487. *
  4488. * ext4_trim_all_free walks through group's buddy bitmap searching for free
  4489. * extents. When the free block is found, ext4_trim_extent is called to TRIM
  4490. * the extent.
  4491. *
  4492. *
  4493. * ext4_trim_all_free walks through group's block bitmap searching for free
  4494. * extents. When the free extent is found, mark it as used in group buddy
  4495. * bitmap. Then issue a TRIM command on this extent and free the extent in
  4496. * the group buddy bitmap. This is done until whole group is scanned.
  4497. */
  4498. static ext4_grpblk_t
  4499. ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
  4500. ext4_grpblk_t start, ext4_grpblk_t max,
  4501. ext4_grpblk_t minblocks)
  4502. {
  4503. void *bitmap;
  4504. ext4_grpblk_t next, count = 0, free_count = 0;
  4505. struct ext4_buddy e4b;
  4506. int ret = 0;
  4507. trace_ext4_trim_all_free(sb, group, start, max);
  4508. ret = ext4_mb_load_buddy(sb, group, &e4b);
  4509. if (ret) {
  4510. ext4_error(sb, "Error in loading buddy "
  4511. "information for %u", group);
  4512. return ret;
  4513. }
  4514. bitmap = e4b.bd_bitmap;
  4515. ext4_lock_group(sb, group);
  4516. if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) &&
  4517. minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
  4518. goto out;
  4519. start = (e4b.bd_info->bb_first_free > start) ?
  4520. e4b.bd_info->bb_first_free : start;
  4521. while (start <= max) {
  4522. start = mb_find_next_zero_bit(bitmap, max + 1, start);
  4523. if (start > max)
  4524. break;
  4525. next = mb_find_next_bit(bitmap, max + 1, start);
  4526. if ((next - start) >= minblocks) {
  4527. ret = ext4_trim_extent(sb, start,
  4528. next - start, group, &e4b);
  4529. if (ret && ret != -EOPNOTSUPP)
  4530. break;
  4531. ret = 0;
  4532. count += next - start;
  4533. }
  4534. free_count += next - start;
  4535. start = next + 1;
  4536. if (fatal_signal_pending(current)) {
  4537. count = -ERESTARTSYS;
  4538. break;
  4539. }
  4540. if (need_resched()) {
  4541. ext4_unlock_group(sb, group);
  4542. cond_resched();
  4543. ext4_lock_group(sb, group);
  4544. }
  4545. if ((e4b.bd_info->bb_free - free_count) < minblocks)
  4546. break;
  4547. }
  4548. if (!ret) {
  4549. ret = count;
  4550. EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
  4551. }
  4552. out:
  4553. ext4_unlock_group(sb, group);
  4554. ext4_mb_unload_buddy(&e4b);
  4555. ext4_debug("trimmed %d blocks in the group %d\n",
  4556. count, group);
  4557. return ret;
  4558. }
  4559. /**
  4560. * ext4_trim_fs() -- trim ioctl handle function
  4561. * @sb: superblock for filesystem
  4562. * @range: fstrim_range structure
  4563. *
  4564. * start: First Byte to trim
  4565. * len: number of Bytes to trim from start
  4566. * minlen: minimum extent length in Bytes
  4567. * ext4_trim_fs goes through all allocation groups containing Bytes from
  4568. * start to start+len. For each such a group ext4_trim_all_free function
  4569. * is invoked to trim all free space.
  4570. */
  4571. int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
  4572. {
  4573. struct ext4_group_info *grp;
  4574. ext4_group_t group, first_group, last_group;
  4575. ext4_grpblk_t cnt = 0, first_cluster, last_cluster;
  4576. uint64_t start, end, minlen, trimmed = 0;
  4577. ext4_fsblk_t first_data_blk =
  4578. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
  4579. ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
  4580. int ret = 0;
  4581. start = range->start >> sb->s_blocksize_bits;
  4582. end = start + (range->len >> sb->s_blocksize_bits) - 1;
  4583. minlen = EXT4_NUM_B2C(EXT4_SB(sb),
  4584. range->minlen >> sb->s_blocksize_bits);
  4585. if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
  4586. start >= max_blks ||
  4587. range->len < sb->s_blocksize)
  4588. return -EINVAL;
  4589. if (end >= max_blks)
  4590. end = max_blks - 1;
  4591. if (end <= first_data_blk)
  4592. goto out;
  4593. if (start < first_data_blk)
  4594. start = first_data_blk;
  4595. /* Determine first and last group to examine based on start and end */
  4596. ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
  4597. &first_group, &first_cluster);
  4598. ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end,
  4599. &last_group, &last_cluster);
  4600. /* end now represents the last cluster to discard in this group */
  4601. end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
  4602. for (group = first_group; group <= last_group; group++) {
  4603. grp = ext4_get_group_info(sb, group);
  4604. /* We only do this if the grp has never been initialized */
  4605. if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
  4606. ret = ext4_mb_init_group(sb, group, GFP_NOFS);
  4607. if (ret)
  4608. break;
  4609. }
  4610. /*
  4611. * For all the groups except the last one, last cluster will
  4612. * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
  4613. * change it for the last group, note that last_cluster is
  4614. * already computed earlier by ext4_get_group_no_and_offset()
  4615. */
  4616. if (group == last_group)
  4617. end = last_cluster;
  4618. if (grp->bb_free >= minlen) {
  4619. cnt = ext4_trim_all_free(sb, group, first_cluster,
  4620. end, minlen);
  4621. if (cnt < 0) {
  4622. ret = cnt;
  4623. break;
  4624. }
  4625. trimmed += cnt;
  4626. }
  4627. /*
  4628. * For every group except the first one, we are sure
  4629. * that the first cluster to discard will be cluster #0.
  4630. */
  4631. first_cluster = 0;
  4632. }
  4633. if (!ret)
  4634. atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen);
  4635. out:
  4636. range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits;
  4637. return ret;
  4638. }