mballoc.c 141 KB

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