mballoc.c 143 KB

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