mballoc.c 133 KB

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