mballoc.c 136 KB

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