mballoc.c 144 KB

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