mballoc.c 138 KB

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