mballoc.c 139 KB

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